﻿using System.Windows;
using System.Windows.Controls;
using SymulatorEthernetWPF.Shared;
using SymulatorEthernetWPF.SimObjects.Devices;

namespace SymulatorEthernetWPF.UserInterface.UIElements
{
    public class CustomCanvas : Canvas
    {
        public CustomCanvas()
        {
            this.MouseMove += CustomCanvas_MouseMove;
        }
        
        void CustomCanvas_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            DoLogic();
        }

        public void DoLogic()
        {
            if (StaticVars.MainWindow != null)
            {
                double maxW = StaticVars.MainWindow.scrollViewer.ViewportWidth / StaticVars.CanvasScale;
                double maxH = StaticVars.MainWindow.scrollViewer.ViewportHeight / StaticVars.CanvasScale;
                foreach (UIElement ui in Children)
                {
                    if (ui.GetType().BaseType == typeof(DeviceControl))
                    {
                        if (GetLeft(ui) + ((DeviceControl)ui).ActualWidth > maxW)
                            maxW = GetLeft(ui) + ((DeviceControl)ui).ActualWidth;
                        if (GetTop(ui) + ((DeviceControl)ui).ActualHeight > maxH)
                            maxH = GetTop(ui) + ((DeviceControl)ui).ActualHeight;
                    }
                }
                Width = maxW * StaticVars.CanvasScale;
                Height = maxH * StaticVars.CanvasScale;
            }
        }
    }
}